#include "FiniteElement.h"
#include <math.h>

FiniteElement::FiniteElement(void)
{
	EndPoint = new Point[2];
	B = new double[3];
	F = new double[2];
}

FiniteElement::FiniteElement(int _index, Point leftPoint, Point rightPoint)
{
	index = _index;
	EndPoint = new Point[2];
	B = new double[3];
	F = new double[2];
	EndPoint[0] = leftPoint;
	EndPoint[1] = rightPoint;
	length = fabs(EndPoint[1].GetCoordinate() - EndPoint[0].GetCoordinate());
}

FiniteElement::~FiniteElement(void)
{
}

int FiniteElement::GetFEIndex()
{
	return index;
}

double FiniteElement::GetLength()
{
	return length;
}

Point FiniteElement::GetMidPoint()
{
	return midPoint;
}

void FiniteElement::operator = (FiniteElement other)
{
	this->index = other.index;
	this->length = other.length;
	this->midPoint = other.midPoint;
	for(int i = 0; i < 2; i++)
		this->EndPoint[i] = other.EndPoint[i];
	for(int i = 0; i < 3; i++)
		this->B[i] = other.B[i];
	for(int i = 0; i < 2; i++)
		this->F[i] = other.F[i];
}

double FiniteElement::funB1(double x) //by variable transform x is contained in (0, 1)
{
	double a = this->EndPoint[0].GetCoordinate();
	double b = this->EndPoint[1].GetCoordinate();
	double h = this->length;
	double y = (b-a)*x + a;

	return h * (p(y)/pow(h, 2.0) + q(y)*pow(x, 2.0));
}

double FiniteElement::funB0(double x)
{
	double a = this->EndPoint[0].GetCoordinate();
	double b = this->EndPoint[1].GetCoordinate();
	double h = this->length;
	double y = (b-a)*x + a;

	return h * (p(y)/pow(h, 2.0) + q(y)*pow(1-x, 2.0));
}

double FiniteElement::funB2(double x)
{
	double a = this->EndPoint[0].GetCoordinate();
	double b = this->EndPoint[1].GetCoordinate();
	double h = this->length;
	double y = (b-a)*x + a;

	return h * (-p(y)/pow(h, 2.0) + q(y)*pow(1-x, 2.0));
}

double FiniteElement::funF1(double x)
{
	double a = this->EndPoint[0].GetCoordinate();
	double b = this->EndPoint[1].GetCoordinate();
	double h = this->length;
	double y = (b-a)*x + a;

	return h * f(y) * (1-x);
}

double FiniteElement::funF0(double x)
{
	double a = this->EndPoint[0].GetCoordinate();
	double b = this->EndPoint[1].GetCoordinate();
	double h = this->length;
	double y = (b-a)*x + a;

	return h * f(y) * x;
}
